package com.easygem.front.service.imp;

import com.easygem.common.model.front.RechargeRecord;
import com.easygem.front.mapper.RechargeRecordMapper;
import com.easygem.front.service.RechargeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;

/**
 * @author 廖思炜
 * @version 1.0
 * @className RechargeRecordImpl
 * @Description TODO
 * @date 2025/3/25 9:14
 */
@Service
public class RechargeRecordImpl implements RechargeRecordService {
    @Autowired
    RechargeRecordMapper rechargeRecordMapper;
    /**
     * 根据条件获取充值记录
     *
     * @param rechargeRecord
     * @return
     */
    @Override
    public List<RechargeRecord> selectByTime(RechargeRecord rechargeRecord) {
        if (rechargeRecord.getAccountName()!=null && !rechargeRecord.getAccountName().isEmpty()) {
            rechargeRecord.setAccountName(rechargeRecord.getAccountName());
        }


        if (rechargeRecord.getStartTime()==null&&rechargeRecord.getStopTime()!=null){
            rechargeRecord.setStartTime(Timestamp.valueOf(LocalDateTime.now()));
        }else if(rechargeRecord.getStartTime()!=null&&rechargeRecord.getStopTime()==null){
            rechargeRecord.setStopTime(Timestamp.valueOf(LocalDateTime.now()));
        }

        // 获取当前的截止时间
        Timestamp originalStopTime = rechargeRecord.getStopTime();
        // 创建一个Calendar实例
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(originalStopTime); // 将Calendar的时间设置为当前截止时间
        // 加一天
        calendar.add(Calendar.DATE, 1);
        // 设置新的截止时间
        rechargeRecord.setStopTime(new Timestamp(calendar.getTimeInMillis()));
        List<RechargeRecord> result = rechargeRecordMapper.selectByTime(rechargeRecord);
        result.sort(Comparator.comparing(RechargeRecord::getCreateTime));
        return result;
    }
}
